﻿// Copyright: 2012 
// Author: Minh Vu - YoungJ
// File name: FilesFolder.cs
// Solution: getlink
// Project: MediaFire
// Time: 2:32 PM 02/05/2012

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Xml;

namespace MediaFire
{
    public class FilesFolder : MyBase
    {

        public void GetFilesFolder(string url)
        {
            var key = url.Substring(url.LastIndexOf("?", StringComparison.Ordinal) + 1);
            var urlXmlFolder = "http://www.mediafire.com/api/folder/get_info.php?folder_key=" + key;

            var client = new WebClient();
            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadProgressChanged += (sender, e) => { Percent = e.ProgressPercentage; };
            client.DownloadStringAsync(new Uri(urlXmlFolder, UriKind.Absolute));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null)
                {
                    Error = e.Error.Message;
                    return;
                }
                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(e.Result);

                var folderNode = xmlDoc.SelectSingleNode("//folder_info");
                if (folderNode == null)
                {
                    Error = "Folder node is empty";
                    return;
                }

                Foldername = folderNode.GetNodeText("name");
                FilesCount = folderNode.GetNodeText("file_count");

                var filesNode = folderNode.SelectNodes("files/file");
                if (filesNode == null)
                {
                    Error = "FilesDownload node is empty";
                    return;
                }

                var data = from XmlNode file in filesNode
                           select new FileDownload
                           {
                               Filename = file.GetNodeText("filename"),
                               QuickKey = file.GetNodeText("quickkey"),
                               Size = file.GetNodeText("size")
                           };
                FilesDownload = data.ToList();
            }
            catch (Exception exception)
            {
                Error = exception.Message;
            }


        }


        private string _foldername;
        public string Foldername
        {
            get { return _foldername; }
            set
            {
                if (string.IsNullOrEmpty(value)) return;
                _foldername = value;
                OnPropertyChanged("Foldername");
            }
        }

        private string _filesCount;
        public string FilesCount
        {
            get { return _filesCount; }
            set
            {
                if (string.IsNullOrEmpty(value)) return;
                _filesCount = value;
                OnPropertyChanged("FilesCount");
            }
        }

        private List<FileDownload> _filesDownload;
        public List<FileDownload> FilesDownload
        {
            get
            {
                return _filesDownload == null ? null : _filesDownload.OrderBy(f => f.Filename).ToList();
            }
            set
            {
                if (value == null) return;
                _filesDownload = value;
                OnPropertyChanged("FilesDownload");
            }
        }


    }
}